package com.quanyan.activity.service.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.quanyan.activity.entity.db.TbActivityMultimedia;
import com.quanyan.activity.entity.db.TbActivityMultimediaExample;
import com.quanyan.activity.entity.vo.req.ReqActivityMediaInfo;
import com.quanyan.activity.entity.vo.res.RespActivityMedia;
import com.quanyan.activity.mapper.TbActivityMultimediaMapper;
import com.quanyan.activity.service.ActivityMediaService;
import com.quanyan.api.APIResponse;
import com.quanyan.common.utils.BeanCopierUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * ActivityMediaServiceImpl
 * @author Li Shuai
 * @date 16/5/28
 */
@Service("mediaService")
public class ActivityMediaServiceImpl implements ActivityMediaService{
    @Autowired
    TbActivityMultimediaMapper tbActivityMultimediaMapper;

    public int addActivityMedia(ReqActivityMediaInfo reqActivityMediaInfo) {
        TbActivityMultimedia tbActivityMultimedia = new TbActivityMultimedia();
        BeanCopierUtils.copyProperties(reqActivityMediaInfo,tbActivityMultimedia);
        tbActivityMultimediaMapper.insertSelective(tbActivityMultimedia);
        return tbActivityMultimedia.getId();
    }

    public int delActivityMedia(Integer mediaId) {
        return tbActivityMultimediaMapper.deleteByPrimaryKey(mediaId);
    }

    public List<RespActivityMedia> queryActivityMediaList(Integer activityId) {
        TbActivityMultimediaExample example = new TbActivityMultimediaExample();
        example.createCriteria().andActivityIdEqualTo(activityId);
        List<TbActivityMultimedia> tbActivityMultimediaList = tbActivityMultimediaMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(tbActivityMultimediaList)){
            return null;
        }
        List<RespActivityMedia> respActivityMediaList = new ArrayList<>();
        RespActivityMedia respActivityMedia = null;
        for (TbActivityMultimedia tbActivityMultimedia : tbActivityMultimediaList){
            respActivityMedia = new RespActivityMedia();
            respActivityMedia.setId(tbActivityMultimedia.getId());
            respActivityMedia.setActivityId(tbActivityMultimedia.getActivityId());
            respActivityMedia.setIsMain(tbActivityMultimedia.getIsMain());
            respActivityMedia.setMultiMediaUrl(tbActivityMultimedia.getMultimediaUrl());
            respActivityMedia.setMediaSize(tbActivityMultimedia.getMediaSize());
            respActivityMedia.setMediaTimeLength(tbActivityMultimedia.getTimeLength());
            respActivityMediaList.add(respActivityMedia);
        }
        return respActivityMediaList;
    }
}

